Skip to content

Conversation

@vbvictor
Copy link
Contributor

@vbvictor vbvictor commented Aug 3, 2025

Addresses #150189.

@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Baranov Victor (vbvictor)

Changes

Addresses #150189.


Full diff: https://github.com/llvm/llvm-project/pull/151862.diff

1 Files Affected:

  • (modified) clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst (+19)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
index bb4f42c88d62f..0e9d5476e7016 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
@@ -59,3 +59,22 @@ Options
    types that don't own the underlying data. Like for `AllowedTypes` above,
    regular expressions are accepted and the inclusion of `::` determines whether
    the qualified typename is matched or not.
+
+
+Limitations
+-----------
+
+This check does not perform lifetime analysis and may suggest replacing copies
+with const references that could become dangling. Be cautious when the
+referenced object might be invalidated by subsequent operations.
+
+.. code-block:: c++
+
+  void consume(const S&);
+
+  void func(std::vector<S> &Vec) {
+    const auto It = Vec.begin();
+    const S Value(*It); // The warning will suggest making this a const reference.
+    Vec.erase(It); // Container modifications could invalidate references.
+    consume(Value); // Safe with copy, dangling reference otherwise.
+  }

@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2025

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

Addresses #150189.


Full diff: https://github.com/llvm/llvm-project/pull/151862.diff

1 Files Affected:

  • (modified) clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst (+19)
diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
index bb4f42c88d62f..0e9d5476e7016 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst
@@ -59,3 +59,22 @@ Options
    types that don't own the underlying data. Like for `AllowedTypes` above,
    regular expressions are accepted and the inclusion of `::` determines whether
    the qualified typename is matched or not.
+
+
+Limitations
+-----------
+
+This check does not perform lifetime analysis and may suggest replacing copies
+with const references that could become dangling. Be cautious when the
+referenced object might be invalidated by subsequent operations.
+
+.. code-block:: c++
+
+  void consume(const S&);
+
+  void func(std::vector<S> &Vec) {
+    const auto It = Vec.begin();
+    const S Value(*It); // The warning will suggest making this a const reference.
+    Vec.erase(It); // Container modifications could invalidate references.
+    consume(Value); // Safe with copy, dangling reference otherwise.
+  }

@vbvictor vbvictor changed the title [clang-tidy][NFC][docs] Add limitation of variable lifetimes in performance-unnecessary-copy-initialization [clang-tidy][docs][NFC] Add limitation of variable lifetimes in performance-unnecessary-copy-initialization Aug 3, 2025
@vbvictor vbvictor merged commit c5e6938 into llvm:main Aug 5, 2025
13 checks passed
@vbvictor vbvictor deleted the add-limitation-performance-unnecessary-copy-initialization branch August 5, 2025 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants